﻿	$(function () {
					var chartPie;
					$(document).ready(function() {
						chartPie = new Highcharts.Chart({
							chart: {
								renderTo: 'graphTabPie',
								marginRight:40,
								marginLeft:50,
								borderColor: '#EBBA95',
								type: 'pie'
							},
							title: {
									text: ''
							},
							credits: {
									text: 'www.logica.fr',
									href: 'http://www.logica.fr/',
									 position: {
										align: 'right',
										x: -30
									}
							},
							 xAxis: {
								title: {
									enabled: true,
									text: '<b>Année</b>',
									margin: 10,
									style: {
										fontWeight: 'normal'
									}
								},
								categories: years,
								
								labels: {
										rotation: -45,
										align: 'right',
										style: {
											fontSize: '10px',
											fontFamily: 'Verdana, sans-serif'
										}
									}
							 },
							 yAxis: {
									min: 0,
									title: {
									   text: 'Nombre de faits'
									}
							},
							tooltip: {
										formatter: function() {
										return '<b style="color:'+this.point.color+'">'+ this.point.name +':</b><br/> '+ this.percentage +' %';
										}
										
								},								  
							 series: [{
									name: "2002",
									data:  getValueByYearsN("2002",["Ain","Aisne","Allier"])
								 }]
						
						});
						for(var $i = 0 ;$i < years.length ; $i++ ){
							$("#select-choice-0").append("<option value='"+years[$i]+"'>"+years[$i]+"</option>");
						}
						for(var $i = 1 ;$i < departments.length ; $i++ ){
							$("#listCheckboxPie").append("<li><input type='checkbox' name='"+departments[$i]+"' id='"+departments[$i]+"' class='custom'/><label for ='"+departments[$i]+"'>"+departments[$i]+"</label></li>");
				
						}
						$("#listCheckboxPie input[name='Ain']").attr("checked","checked");
						$("#listCheckboxPie input[name='Aisne']").attr("checked","checked");
						$("#listCheckboxPie input[name='Allier']").attr("checked","checked");
						
						$("#listCheckboxPie input[type='checkbox']").bind( "change", function(event, ui) {
							var $d = $(this).attr('name');
							if($(this).is(':checked')){
								$(this).attr("checked","checked");
								var $y = $("#select-choice-0 option:selected").text();
								chartPie.series[0].addPoint({ y: data[$y][$d], id : $d , name : $d});
							}else{
								chartPie.get($(this).attr("name")).remove();
								$(this).removeAttr("checked");
							}
						 });
						 
						 $("#select-choice-0").bind( "change", function(event, ui) {
							var $value=$(this).children("option:selected").text();
							var tabCheckB = $("#listCheckboxPie input[type='checkbox'][checked]");
							console.log(tabCheckB);
							var t=[];
							for(var $i=0;$i<tabCheckB.length;$i++){
								t.push(tabCheckB[$i]['id']);
							}
							chartPie.series[0].setData(getValueByYearsN($value,t));
						});
					});
					
					
				});